package TUILayer;
import java.util.Scanner;
import ControllerLayer.*;

/**
 * Write a description of class ProductNo here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class ProductNoUI
{
    // instance variables - replace the example below with your own
    private ProductController pCtr;

    /**
     * Constructor for objects of class ProductNo
     */
    public ProductNoUI()
    {
        // initialise instance variables
        pCtr = ProductController.getInstance();
    }

    public void start()
    {
        Menu();
    }

    public void Menu()
    {
        boolean exit = false;

        while(!exit)  //if exit = true, the main menu will shut down 
        {
            int choice = writeMainMenu();
            switch(choice)
            {
                case 1:
                create();
                break;
                case 2:
                read();
                break;
                case 3:
                update();
                break;                
                case 4:
                delete();
                break;
                case 5:
                printID();
                break;
                case 6:
                printAll();
                break;                
                default:                
                exit = true;
                break; 
            }
        }
    }

    public int writeMainMenu()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("\f ****Item Menu***");
        System.out.println("  (1) Create");
        System.out.println("  (2) Read");
        System.out.println("  (3) Update");
        System.out.println("  (4) Delete");
        System.out.println("  (5) Print ID");
        System.out.println("  (6) Print ALL");        
        System.out.println("  (7) Return");
        System.out.println("\n make your choice: ");

        int choice = keyboard.nextInt();

        return choice;

    }

    private long inputIDPD()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        long out = 0;
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Product Description ID: " );
        while(!ok)
        {
            try{
                out = keyboard.nextLong();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }
        return out;
    }  

    private long inputIDPN()
    {
        // creates an object keyboard to read data from the keyboard
        boolean ok = false;
        long out = 0;    
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type Item ID: " );
        while(!ok)
        {
             try{
                out = keyboard.nextLong();
                ok = true;
            }
            catch(Exception e)
            {
                System.out.println("You must enter a number -  try again");
                String input = keyboard.nextLine();
            }
        }
       
        return out;
    }  

    private boolean inputStoreStatus()
    {
        // creates an object keyboard to read data from the keyboard
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Type if active or not (y/n): " );
        String answer = keyboard.nextLine();
        if(answer == "y")
            return true;
        else 
            return false;
    }    

    public void create()
    {
        Scanner keyboard = new Scanner(System.in);

        long idPd = inputIDPD(); //type product description id        
        if(pCtr.readProduct(idPd) != null)
        {
            pCtr.createProductNo(idPd, inputStoreStatus());        
            long idPn = pCtr.getStoreNo(); //get created item id of product description
            System.out.println("       new item to Product Description has been created - se info above");
            pCtr.printOneProductNo(idPd, idPn);   
        }
        else
        {
            System.out.println("     Product Description was not found - try another ID");
            System.out.println("..to find item the product description ID is required");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void read()
    {        
        Scanner keyboard = new Scanner(System.in);

        long idPd = inputIDPD();
        long idPn = inputIDPN();        

        if(pCtr.readProduct(idPd) != null)
        {
            System.out.println("    Product Description was found - se info above");
            pCtr.printOneProduct(idPd);
            System.out.println("  Product code nr.: "+pCtr.readProduct(idPd));
        }
        else
        {
            System.out.println("     Product Description was not found - try another ID");
            System.out.println("..to find item the product description ID is required");
        }

        if(pCtr.readProduct(idPd) != null && pCtr.readProductNo(idPd, idPn) != null)
        {
            System.out.println("Product description containing: ");
            System.out.println("     Item of Description was found - se info above");
            pCtr.printOneProductNo(idPd, idPn);
            System.out.println("  Item nr.: "+pCtr.readProductNo(idPd, idPn));
        }
        else
        {
            System.out.println("     Item was not found - try another ID");
        }        

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void update()
    {
        Scanner keyboard = new Scanner(System.in);

        long idPd = inputIDPN();
        long idPn = inputIDPN();

        if(pCtr.readProduct(idPd) != null)
        {
            System.out.println("    Product Description was found - se info above");
            pCtr.printOneProduct(idPd);
            System.out.println("  Product code nr.: "+pCtr.readProduct(idPd));
        }
        else
        {
            System.out.println("     Product Description was not found - try another ID");
            System.out.println("..to find item the product description ID is required");
        }

        if(pCtr.readProduct(idPd) != null && pCtr.readProductNo(idPd, idPn) != null)
        {
            pCtr.updateProductNo(idPd, idPn, inputStoreStatus());
            System.out.println("Product description containing: ");
            System.out.println("     Item of Description has been updated - se info above");
            pCtr.printOneProductNo(idPd, idPn);
            System.out.println("  Item nr.: "+pCtr.readProductNo(idPd, idPn));
        }
        else
        {
            System.out.println("     Item was not found - try another ID");
        }    

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void delete()
    {
        Scanner keyboard = new Scanner(System.in);

        long idPd = inputIDPN();
        long idPn = inputIDPN();

        if(pCtr.readProduct(idPd) != null)
        {
            System.out.println("    Product Description was found - se info above");
            pCtr.printOneProduct(idPd);
            System.out.println("  Product code nr.: "+pCtr.readProduct(idPd));
        }
        else
        {
            System.out.println("     Product Description was not found - try another ID");
            System.out.println("..to find item the product description ID is required");
        }

        if(pCtr.readProduct(idPd) != null && pCtr.readProductNo(idPd, idPn) != null)
        {
            System.out.println("Product description containing: ");
            System.out.println("     Item of Description is deleted - se info above");         
            pCtr.printOneProductNo(idPd, idPn);
            System.out.println("  Item nr.: "+pCtr.readProductNo(idPd, idPn));
            pCtr.deleteProductNo(idPd, idPn);
        }
        else
        {
            System.out.println("     Item was not found - try another ID");
        }   

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printID()
    {
        Scanner keyboard = new Scanner(System.in);

        long idPd = inputIDPN();
        long idPn = inputIDPN();

        if(pCtr.readProduct(idPd) != null)
        {
            System.out.println("    Product Description was found - se info above");
            pCtr.printOneProduct(idPd);
            System.out.println("  Product code nr.: "+pCtr.readProduct(idPd));
        }
        else
        {
            System.out.println("     Product Description was not found - try another ID");
            System.out.println("..to find item the product description ID is required");
        }

        if(pCtr.readProduct(idPd) != null && pCtr.readProductNo(idPd, idPn) != null)
        {
            System.out.println("Product description containing: ");
            System.out.println("     Item of Description - se info above");         
            pCtr.printOneProductNo(idPd, idPn);
            System.out.println("  Item nr.: "+pCtr.readProductNo(idPd, idPn));            
        }
        else
        {
            System.out.println("     Item was not found - try another ID");
        } 

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }

    public void printAll()
    {
        Scanner keyboard = new Scanner(System.in);

        long idPd = inputIDPN();        

        if(pCtr.readProduct(idPd) != null)
        {
            System.out.println("    Product Description was found - se info above");
            pCtr.printOneProduct(idPd);
            System.out.println("  Product code nr.: "+pCtr.readProduct(idPd));
        }
        else
        {
            System.out.println("     Product Description was not found - try another ID");
            System.out.println("..to find item the product description ID is required");
        }

        if(pCtr.readProduct(idPd) != null && pCtr.getPnSize(idPd) > 0)
        {
            System.out.println("Product description containing: ");
            System.out.println("     Item of Description - se info above");         
            pCtr.printAllProductNo(idPd);                        
        }
        else
        {
            System.out.println("     Item was not found - try another ID");
        }

        System.out.println();
        System.out.println("press any to continue");
        keyboard.nextLine();
    }
}
